/**
 * @(#)SellerAjaxController.java, 2018/2/6.
 * <p/>
 * Copyright 2018 Kerr Wang, Inc. All rights reserved.
 * KERRWANG PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package org.wangzx.exercise.controller.xhr;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.wangzx.exercise.enums.LoginType;
import org.wangzx.exercise.meta.vo.ItemVO;
import org.wangzx.exercise.meta.vo.UserVO;
import org.wangzx.exercise.meta.vo.common.AjaxResult;
import org.wangzx.exercise.meta.vo.common.ResponseCode;
import org.wangzx.exercise.service.basic.ItemService;
import org.wangzx.exercise.service.core.UserCoreService;
import org.wangzx.exercise.utils.LoginUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @author hzwangzuxiang(ahscwzx@163.com)
 */
@Controller
@RequestMapping("/")
public class SellerAjaxController extends BaseAjaxController {
    static private final Logger logger = LoggerFactory.getLogger(SellerAjaxController.class);

    @Autowired
    private ItemService itemService;

    @Autowired
    private UserCoreService userCoreService;

    @RequestMapping("/publicSubmit.json")
    @ResponseBody
    public AjaxResult publicSubmit(@RequestBody ItemVO itemVO, HttpServletRequest request) {
        logger.info("[op:publicSubmit] itemVO={}", JSON.toJSONString(itemVO));

        UserVO userVO = LoginUtils.loginCheck(request);
        if(userVO.getLoginType() != LoginType.SELLER.getValue()) {
            return initFailureResult(ResponseCode.UNAUTHORIZED);
        }
        long sellerId = userCoreService.getByName(userVO.getUserName()).getId();
        return initSuccessResult(itemService.insertItem(itemVO, sellerId));
    }
}
